/*******************************************************************************
 * Copyright (c) 2006 CEA List.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     CEA List - initial API and implementation
 *******************************************************************************/
package com.cea.papyrus.core.ui.properties.advanced;

import java.util.Collection;
import java.util.Iterator;

import org.eclipse.jface.viewers.ILabelProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.uml2.uml.LiteralSpecification;
import org.eclipse.uml2.uml.NamedElement;

/**
 * This class is used to Display the Ereference.
 * 
 * @author PT202707
 */
public class ReferenceLabelProvider extends LabelProvider implements ILabelProvider{
	
	/**
	 * 
	 */
	public ReferenceLabelProvider(){
		super();
	}
	
	
	/**
	 * 
	 * 
	 * @param element 
	 * 
	 * @return 
	 */
	@Override
	public String getText(Object element) {
		if (element!=null){	
			if (element instanceof String) {
				return ((String)element);
			}
			if (element instanceof LiteralSpecification) {
				return ((LiteralSpecification)element).stringValue();
			}
			if (element instanceof NamedElement) {
				return ((NamedElement)element).getName();
			}
			if (element instanceof Collection){
				String out="[";
				Iterator it =((Collection)element).iterator();
				while(it.hasNext()){
					Object e =it.next();
					if (e instanceof String) {
						out=out+ ((String)e);
					} else if (e instanceof NamedElement) {
						out=out+ ((NamedElement)e).getName();
					} else{
						String classname= e.getClass().toString();
						int i=classname.lastIndexOf('.');
						classname= classname.substring(i+1, classname.length());
						out=out+ classname+e.hashCode();}
				if	(it.hasNext()){
					out=out+", ";}
				}
				return out+"]";
			}
			String classname= element.getClass().toString();
			int i=classname.lastIndexOf('.');
			classname= classname.substring(i+1, classname.length());
			return ""+classname+element.hashCode();
		}
		else{
			return "unspecified"; }
	}
}